#include "comm.h"

#define SERV_PORT 10000

void str_cli(FILE *fin, int sockfd, struct sockaddr *serv_addr, socklen_t addrlen);
int main(int ac , char *av[]){
    int sockfd;
    struct sockaddr_in serv_addr;
    if(ac==1){
        printf("usage: %s server\n", av[0]);
        exit(0);
    }
    memset(&serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_port = htons(SERV_PORT);
    serv_addr.sin_family = AF_INET;
    if(inet_pton(AF_INET, av[1], &serv_addr.sin_addr)<=0){
        err_sys();
    }
    if((sockfd=socket(AF_INET, SOCK_DGRAM, 0))<0){
        err_sys();
    }
    str_cli(stdin, sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));


    return 0;
}


void str_cli(FILE *fin, int sockfd, struct sockaddr *serv_addr, socklen_t addrlen){
    char buf[MAXLINE];
    int n;
    while(fgets(buf, MAXLINE, fin)){
        if((n=sendto(sockfd, buf, sizeof(buf), 0, serv_addr, addrlen))<0){
            err_sys();
        }
        if((n=recvfrom(sockfd, buf, sizeof(buf), 0, serv_addr, &addrlen))<0){
            err_sys();
        }
        buf[n] = '\0';
        fputs(buf, stdout);
    }
}
        


